package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.*;
import com.yuke.cloud.service.pmc.entity.ProdRegionIntroduce;
import com.yuke.cloud.service.pmc.entity.ProdRegionPicture;
import com.yuke.cloud.service.pmc.entity.ProdRegionResortIntroduce;
import com.yuke.cloud.service.pmc.entity.ProdRegionResortPicture;
import com.yuke.cloud.service.pmc.service.ProdRegionIntroduceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.pmc.service.ProdRegionPictureService;
import com.yuke.cloud.service.pmc.service.ProdRegionResortIntroduceService;
import com.yuke.cloud.service.pmc.service.ProdRegionResortPictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zys on 2018/09/30.
*/
@Slf4j
@Api(value = "WEB - ProdRegionIntroduceController", tags = "处理ProdRegionIntroduce相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/region_introduce")
public class ProdRegionIntroduceController extends BaseController {
    @Resource
    private ProdRegionIntroduceService prodRegionIntroduceService;
    @Resource
    private ProdRegionPictureService prodRegionPictureService;
    @Resource
    private ProdRegionResortIntroduceService prodRegionResortIntroduceService;
    @Resource
    private ProdRegionResortPictureService prodRegionResortPictureService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodRegionIntroduce", value = "该数据记录的POJO") @RequestBody ProdRegionIntroduce prodRegionIntroduce) {
        Example example = new Example(ProdRegionIntroduce.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("regionId", prodRegionIntroduce.getRegionId());
        if (prodRegionIntroduceService.selectCountByExample(example) > 0) {
            return  WrapMapper.error("该地域介绍已存在");
        }
        prodRegionIntroduceService.save(prodRegionIntroduce);
        if (prodRegionIntroduce.getProdRegionPictures() != null && prodRegionIntroduce.getProdRegionPictures().size() > 0) {
            prodRegionIntroduce.getProdRegionPictures().forEach(prodRegionPicture -> prodRegionPictureService.save(prodRegionPicture));
        }
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        prodRegionIntroduceService.deleteById(id);
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据ids删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "factoryIds", value = "记录id") @RequestBody ProdIdsDto prodIdsDto) {
        prodRegionIntroduceService.deleteByIds(prodIdsDto.getIds());
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodRegionIntroduce", value = "需要修改的内容") @RequestBody ProdRegionIntroduce prodRegionIntroduce) {
        prodRegionIntroduceService.update(prodRegionIntroduce);

        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdRegionIntroduce> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdRegionIntroduce prodRegionIntroduce = prodRegionIntroduceService.findById(id);
        ProdRegionPicture prodRegionPicture = new ProdRegionPicture();
        prodRegionPicture.setRegionId(prodRegionIntroduce.getRegionId());
        prodRegionIntroduce.setProdRegionPictures(prodRegionPictureService.select(prodRegionPicture));

//        ProdRegionDto prodRegionDto = new ProdRegionDto();
//        prodRegionDto.setProdRegionIntroduce(prodRegionIntroduce);

//        ProdRegionResortIntroduce prodRegionResortIntr = new ProdRegionResortIntroduce();
//        prodRegionResortIntr.setRegionId(prodRegionIntroduce.getRegionId());
//        ProdRegionResortIntroduce prodRegionResortIntroduce = prodRegionResortIntroduceService.selectOne(prodRegionResortIntr);
//        ProdRegionResortPicture prodRegionResortPicture = new ProdRegionResortPicture();
//        prodRegionResortPicture.setRegionId(prodRegionIntroduce.getRegionId());
//        prodRegionResortIntroduce.setProdRegionResortPictures(prodRegionResortPictureService.select(prodRegionResortPicture));
//
//        prodRegionDto.setProdRegionResortIntroduce(prodRegionResortIntroduce);

        return WrapMapper.ok(prodRegionIntroduce);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdRegionIntroduce>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdRegionIntroduce> list = prodRegionIntroduceService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /*@GetMapping("/app/recommend")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<List<ProdRegionRecommendDto>> recommendDetail() {
        List<ProdRegionRecommendDto> prodRegionRecommendDtos = prodRegionIntroduceService.getRecommendDetail();
        return WrapMapper.ok(prodRegionRecommendDtos);
    }

    @GetMapping("/app/{cityId}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdRegionInfoDto> detailByCityId(@ApiParam(name = "cityId", value = "记录id") @PathVariable Long cityId) {
        ProdRegionInfoDto prodRegionInfoDto = prodRegionIntroduceService.detailByCityId(cityId);
        return WrapMapper.ok(prodRegionInfoDto);
    }*/

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper<PageInfo<ProdRegionIntroduceDto>> getListDetailByCondition(@ApiParam(name = "prodBrand", value = "记录prodBrand") @RequestBody(required = false) ProdRegionIntroduce prodRegionIntroduce,
                                                              @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                              @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdRegionIntroduceDto> list = prodRegionIntroduceService.getListDetailByCondition(prodRegionIntroduce);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/list")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper<List<ProdRegionIntroduce>> getListByCondition(@ApiParam(name = "prodBrand", value = "记录prodBrand") @RequestBody(required = false) ProdRegionIntroduce prodRegionIntroduce) {
        return WrapMapper.ok(prodRegionIntroduceService.getListByCondition(prodRegionIntroduce));
    }
}
